<template>
    <div style="padding: 10px">
        <el-card class="box-card" shadow="hover">
            <!--新增区域-->
            <div style="margin: 10px 0" v-if="user.role===1">
                <el-button type="primary" @click="add">新增</el-button>
            </div>

            <!--搜索区域-->
            <div style="margin: 10px 0">
                <el-input v-model="pageInfo.query" placeholder="按标题查询" style="width: 20%" clearable></el-input>
                <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
            </div>

            <!--数据表格-->
            <el-table :data="tableData" stripe border style="width: 100%">
                <el-table-column prop="id" label="ID" sortable></el-table-column>
                <el-table-column prop="title" label="标题"></el-table-column>
                <el-table-column prop="author" label="作者"></el-table-column>
                <el-table-column prop="time" label="时间"></el-table-column>
                <el-table-column label="详情">
                    <template #default="scope">
                        <el-button size="mini" type="success" @click="detalts(scope.row)">详情</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" v-if="user.role===1">
                    <template #default="scope">
                            <el-button size="mini" type="warning" @click="handleEdit(scope.row)">编辑</el-button>
                            <el-popconfirm title="确认删除吗？" @confirm="handleDelete(scope.row.id)">
                                <template #reference>
                                    <el-button size="mini" type="danger">删除
                                    </el-button>
                                </template>
                            </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>

            <!--分页-->
            <div style="margin: 10px 0">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pageInfo.pageNum"
                        :page-sizes="[5, 10, 20]"
                        :page-size="pageInfo.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
        </el-card>

        <!--添加按钮的弹出框-->
        <el-dialog title="提示" v-model="dialogVisible" width="50%">
            <el-form :model="form" label-width="120px">
                <el-form-item label="标题">
                    <el-input v-model="form.title" style="width: 50%"></el-input>
                </el-form-item>
                <!--<el-form-item label="内容">
                    <el-input v-model="form.content" style="width: 80%"></el-input>
                </el-form-item>-->
                <div id="div1"></div>
            </el-form>
            <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
            </template>
        </el-dialog>

        <el-dialog title="详情" v-model="vis" width="50%">
            <el-card>
                <div v-html="detalt.content" style="min-height: 100px"></div>
            </el-card>
        </el-dialog>
    </div>
</template>

<script>


    import request from "../utils/request";
    import E from "wangeditor"
    let editor


    export default {
        name: 'News',
        components: {},
        data() {
            return {
                pageInfo: {
                    query: '',
                    pageNum: 1,
                    pageSize: 5,
                },
                total: 0,
                tableData: [],
                dialogVisible: false,
                form: {},
                detalt: {},
                vis: false,
                user:{},
            }
        },
        methods: {
            detalts(row) {
                this.detalt = row
                this.vis = true
            },
            async load() {
                const {data: result} = await request.get("/news/list", {params: this.pageInfo})
                this.tableData = result.rows
                this.total = result.total
            },
            add() {
                this.form = {}
                this.dialogVisible = true
                this.$nextTick(() => {
                    // 关联弹窗里面的div，new一个 editor对象
                    if (!editor) {
                        editor = new E('#div1')
                        // 配置 server 接口地址
                        editor.config.uploadImgServer = 'http://localhost:9090/files/editor/upload'
                        editor.config.uploadFileName = "file"  // 设置上传参数名称
                        editor.create()
                    }
                    editor.txt.html("")
                })
            },
            async save() {
                this.form.content = editor.txt.html()
                let userStr = sessionStorage.getItem("user") || "{}"
                let user = JSON.parse(userStr)
                if (this.form.id) {//更新
                    const result = await request.put("/news", this.form)
                    this.dialogVisible = false
                    this.load();
                    if (result.code === '0') {
                        this.$message({
                            type: "success",
                            message: "更新成功"
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: result.msg
                        })
                    }

                } else {//新增
                    this.form.author = user.nickName
                    const result = await request.post("/news", this.form)
                    this.dialogVisible = false
                    this.load();
                    if (result.code === '0') {
                        this.$message({
                            type: "success",
                            message: "新增成功"
                        })
                        editor.txt.clear()
                    } else {
                        this.$message({
                            type: 'error',
                            message: result.msg
                        })
                    }
                }
            },
            handleEdit(row) {
                this.form = JSON.parse(JSON.stringify(row))
                this.dialogVisible = true
                this.$nextTick(() => {
                    // 关联弹窗里面的div，new一个 editor对象
                    // 关联弹窗里面的div，new一个 editor对象
                    if (!editor) {
                        editor = new E('#div1')
                        // 配置 server 接口地址
                        editor.config.uploadImgServer = 'http://localhost:9090/files/editor/upload'
                        editor.config.uploadFileName = "file"  // 设置上传参数名称
                        editor.create()
                    }
                    editor.txt.html(row.content)
                })
            },
            handleDelete(id) {
                request.delete("/news/delete/" + id).then(result => {
                    if (result.code === '0') {
                        this.$message({
                            type: "success",
                            message: "删除成功！"
                        })
                    } else {
                        this.$message({
                            type: "error",
                            message: result.msg
                        })
                    }
                    this.load()
                    console.log(id)
                })


            },
            handleSizeChange(pageSize) {
                this.pageInfo.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {
                this.pageInfo.pageNum = pageNum
                this.load()
            },
            async checkUser(){
                const result=await request.get("/user/"+this.user.id)
                if (result==='0'){
                    this.user=result.data
                }
            }
        },
        created() {
            this.load()
            let userStr = sessionStorage.getItem("user") || "{}"
            this.user=JSON.parse(userStr)
        }

    }
</script>
